package com.wlstock.funi.utils;

import android.content.Context;

import com.wlstock.funi.model.OperateModel;
import com.wlstock.funi.model.SelfFundModel;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.List;
import java.util.Stack;


public class CaculateUtils {
	
	private int  TOTAL_MONEY = 100000;
	private static HashMap<String, String> dateHashes;
	
	public static String getTradeTips(String tradeMark, float tradePrice, float offset){
		if (tradeMark.equalsIgnoreCase("建仓")) {
			return tradeMark + String.valueOf(getBuyPrice(tradePrice,offset));
		} else if (tradeMark.equalsIgnoreCase("清仓")) {
			return tradeMark;
		}
		return "";
	}
	
	public static HashMap<String, Integer> getModelRangeFromList(OperateModel model, List<OperateModel> models){
		HashMap<String, Integer> r = new HashMap<String, Integer>();
		
		int nums = 0;//人数
		List<OperateModel> tempModels = filterRepeatModels(models);
		
		for (int i = 0; i < tempModels.size(); i++) {
			//必须是买入的才能算在里面
			if (tempModels.get(i).getStockHoldDetailId()!=null && tempModels.get(i).getTradeType() == OperateModel.TradeType_BUY) {
				nums++;
			}
			if (tempModels.get(i).getFundid()== model.getFundid()) {
				//i表示第几名
				r.put("range", i+1);
			}
		}
		//nums表示那一期的数目
		r.put("num", nums);
		return r;
	}

	/**
	 * 获取持仓人数
	 * @param models
	 * @return
     */
	public static int yiMengkeepCount(List<OperateModel> models){
		int nums = 0;//人数
		List<OperateModel> tempModels = filterRepeatModels(models);

		for (int i = 0; i < tempModels.size(); i++) {
			//必须是买入的才能算在里面
			if (tempModels.get(i).getStockHoldDetailId()!=null && tempModels.get(i).getTradeType() == OperateModel.TradeType_BUY) {
				nums++;
			}
		}
		return nums;
	}

	public static List<OperateModel> filterRepeatModels(List<OperateModel> models) {
		List<OperateModel> tempModels = new ArrayList<OperateModel>();
		
		for (int i = 0; i < models.size()-1; i++) {
			if (models.get(i).getFundid() == models.get(i+1).getFundid()) {
				tempModels.add(models.get(i));
			}
			//处理最后一个元素
			if (i+1 == models.size()-1) {
				tempModels.add(models.get(i+1));
			}
		}
		return tempModels;
	}
	
	
	/**
	 * 
	 * @param date1 第一期开始的日期
	 * @param date2 现在这一期对上一期的日期
	 * @return
	 */
	public static Stack<String> getSeasons(Date date1, Date date2) {
		Calendar cal1 = new GregorianCalendar();
		cal1.setTime(date1);
		Calendar cal2 = new GregorianCalendar();
		cal2.setTime(date2);
		int c = (cal1.get(Calendar.YEAR) - cal2.get(Calendar.YEAR)) * 12 + cal1.get(Calendar.MONTH)- cal2.get(Calendar.MONTH);
		Stack<String> stack = new Stack<String>();
		
		//c-1是因为少了一期， c-2取现在一期的上一期
		for (int i = c-2; i >=0; i--) {
			stack.push(String.valueOf(i+6));
		}
		return stack;
	}
	
	
	public static String getSeasonDate(String seanson){
		if (dateHashes == null) {
			dateHashes = new HashMap<String, String>();
			dateHashes.put("6",  "2012-");
			dateHashes.put("7",  "2012-");
			dateHashes.put("8",  "2012-");
			dateHashes.put("9",  "2012-");
			dateHashes.put("10", "2013-");
			dateHashes.put("11", "2013-");
			dateHashes.put("12", "2013-");
			dateHashes.put("13", "2013-");
			dateHashes.put("14", "2013-");
			dateHashes.put("15", "2013-");
			dateHashes.put("16", "2013-");
			dateHashes.put("17", "2013-");
			dateHashes.put("18", "2013-");
			dateHashes.put("29", "2013-");
			dateHashes.put("20", "2013-");
			dateHashes.put("21", "2013-");
			dateHashes.put("22", "2014-");
			dateHashes.put("23", "2014-");
			dateHashes.put("24", "2014-");
			dateHashes.put("25", "2014-");
			dateHashes.put("26", "2014-");
			dateHashes.put("27", "2014-");
			dateHashes.put("28", "2014-");
			dateHashes.put("29", "2014-");
			dateHashes.put("30", "2014-");
			dateHashes.put("31", "2014-");
			dateHashes.put("32", "2014-");
			dateHashes.put("33", "2014-");
			dateHashes.put("34", "2015-");
			dateHashes.put("35", "2015-");
			dateHashes.put("36", "2015-");
			dateHashes.put("37", "2015-");
			dateHashes.put("38", "2015-");
			dateHashes.put("39", "2015-");
			dateHashes.put("40", "2015-");
			dateHashes.put("41", "2015-");
			dateHashes.put("42", "2015-");
			dateHashes.put("43", "2015-");
			dateHashes.put("44", "2015-");
			dateHashes.put("45", "2015-");
		}
		return dateHashes.get(seanson);
	}
	
	
	//获取总仓位 一直都是以TOTAL_MONEY作为基础
	private static long getTotalPositon(int fundNums){
		return 0;
	}
	
	/**
	 * 获取交易价格与当前价格的差距百分比
	 * @param model
	 * @return
	 */
	public static float getCurPriceDis2TrPrice(SelfFundModel model) {
		return getFloatDis(model.getCurPrice(), model.getTradePrice());
	}
	
	/**
	 * 
	 * @param numerator 现在价格或点数
	 * @param denominator 昨天价格、点数 或 成交价格等
	 * @return
	 */
	public static float getFloatDis(float numerator,float denominator){
		float offset = (numerator-denominator) / denominator;
		return offset;
	}
	
	/**
	 * 获取变化的绝对值,返回的是百分数为单位
	 * @param numerator 现在价格或点数
	 * @param denominator 昨天价格、点数 或 成交价格等
	 * @return
	 */
	public static float getFloatDisABS(float numerator,float denominator){
		float offset = Math.abs((numerator-denominator)) / denominator;
		return offset;
	}
	
	/**
	 * 返回百分数为单位
	 * @param numerator 现在价格／点数
	 * @param denominator 昨天价格／点数 或 成交价格等
	 * @return
	 */
	public static float getFloatDis(String numerator, String denominator) {
		try {
			return getFloatDis(Float.valueOf(numerator), Float.valueOf(numerator));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 0;
	}
	
	public static String getPercentByFloat(float value){
		DecimalFormat df = new DecimalFormat("0.00%");
		return df.format(value);
	}
	
	/**
	 * 通过百分比获取浮点数
	 * @param percent
	 * @return
	 */
	public static float getFloatByPercent(String percent){
		try {
			return Float.valueOf(percent)/100;
		} catch (Exception e) {
			// TODO: handle exception
		}
		return 0.0f;
	}
	
	/**
	 * 计算value 偏移 offset 的值
	 * @param value
	 * @param offset
	 * @return
	 */
	public static float caculateFloatValue(float value,float offset){
		return getBuyPrice(value, offset);
	}
	
	public static float getBuyPrice(float tradePrice,float offset){
		double f = tradePrice + tradePrice*offset*0.01;
		BigDecimal bd = new BigDecimal(f);  
	    bd = bd.setScale(2 ,BigDecimal.ROUND_UP);  
	    return (float)bd.doubleValue();
	}
	
	public static float getSellPrice(float tradePrice,float offset){
		double f = tradePrice + tradePrice*offset*0.01;
		BigDecimal bd = new BigDecimal(f);  
	    bd = bd.setScale(2 ,BigDecimal.ROUND_UP);  
	    return (float)bd.doubleValue();
	}
	
//	public static String getTradePty (int position, int fundNums,float price){
//		// 这里暂时不需要考虑fundNums的问题,因为getTradeMoney 已经考虑了
//		float nums = getTradeMoney(position, fundNums)/Float.valueOf(price);
//		return scaleNums(nums);
//	}

	/**
	 * 根据当期的操盘持仓情况以及设置中的fundid
	 * @param mContext
	 * @param fundId   操盘手id
	 * @param price    经过计算买入价格
	 * @param traders  当期的买入人数
	 * @return
	 */
	public static long getTradePty(Context mContext,int fundId,float price,int traders){
		if(traders < 9){
			return 0;
		}
		float nums = TradeSetUtils.getTradeMoney(mContext, fundId)/price;
		return scaleNums(nums);
	}
	
	/**
	 * 根据固定的金钱计算股票数
	 * @param totalMoney
	 * @param price
	 * @return
	 */
	public static long caculateNums(float totalMoney, float price){
		float nums = totalMoney / price;
		return scaleNums(nums);
	}
	
	/**
	 * 计算股票数
	 * @param nums
	 * @return
	 */
	public static long scaleNums(float nums) {
		nums = nums/100;
		if (nums == 0) {
			return  0;
		}
		BigDecimal b = new BigDecimal(nums);
		b = b.setScale(0, RoundingMode.DOWN);
		long pty ;
		if (0 <b.doubleValue()&& b.doubleValue() < 1) {
			pty = 100;
		} else {
			pty = (long) b.doubleValue()*100;
		}
		return (pty);
	}
	
//	//根据排名选择交易金额
//	public static float getTradeMoney (int position, int fundNums) {
//		
//		if (fundNums < 8) {
//			//交易人数小于8个人，直接返回
//			return .0f;
//		} 
//		
//		if (TradeSetUtils.getTrackMode(FundApplication.getInstance())==TradeSetUtils.TRACK_ALL) {
//			if (position < 3) {//前2名
//				return 8000.0f;
//			} else if (position < 6) {// 4 - 5
//				return 5000.f;
//			} else if (position < 8) {// 
//				return 3000.f;
//			} else if (position < 11) {//7-10
//				return 0.f;
//			}
//		} else {
//			//只跟踪前5名
//			if (position < 3) {//前3名
//				return 9000.0f;
//			} else if (position < 6) {//3 － 6
//				return 6000.f;
//			} 
//		}
//		return .0f;
//	}
	
	//获取可信度
	/**
	 * 
	 * @param range      本期收益排名    权重比0.7
	 * @param fundNums   本期建仓人数    权重比0.3,需要fundNums大于3的时候才有用
	 *
	 * @return
	 * 	10w  
		权重比 
		本期收益 > 操盘风格  > 准确率 > 总收益 
		  0.5            0.3           0.15      0.05
		1.4+1.6+1.8+3.5+4.5 ＝ 12.8
		10w 初始资金
		本期收益排行   本期建仓人数
		1              3    10.93％
		2              2    12.5%
		3              1    14.06
		4              5    27.34
		5              4    35.15
	 * 
	 */
	private static double getConfidence(int profitRateRange ,int fundNums){
		//本期收益排名、建仓人数（操盘风格、准确率、总收益）需要fundNums大于3的时候才有用
		if (fundNums < 3) {
			return 0.0;
		} else {
			
		}
		return 1.0;
	}

	/**
	 * 将市场金额缩放到以亿为单位
	 * @param money
	 * @return
     */
	public static  String scaleMoneyFlow(float money){
		money = money/100000000;
		BigDecimal bd = new BigDecimal(money);
		bd = bd.setScale(3 ,BigDecimal.ROUND_UP);
		return String.valueOf(bd.floatValue());
	}
	
}
